package com.etrans.web.gps.timer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.util.EgovUtil;

public class OnlineVehicle {
	@Autowired
    private EgovUtil egovUtil;
	
    @Autowired
    private CommonService commonService;
	
	public void getOnline() {
		long start = System.currentTimeMillis();
		try {
			//加载所有车辆
			List<HashMap<String,Object>> vehicles = commonService.queryVeListByRole(null);
			String[] vehicleArray = new String[vehicles.size()];
			for (int i = 0; i < vehicles.size(); i++) {
				String vehicle_no = String.valueOf(vehicles.get(i).get("vehicle_no"));
    			String vehicle_color = String.valueOf(vehicles.get(i).get("vehicle_color"));
    			//车牌号_车牌颜色添加到数组
    			vehicleArray[i] = String.format("%s_%s", vehicle_no, vehicle_color);
			}
			//获取车辆状态
			Map<String,List<String>> vehicleStatus = egovUtil.getVehicleStatus(vehicleArray);
			//缓存在线车辆
			Set<String> onlineList = new HashSet<String>();
			if(vehicleStatus != null){
				onlineList.addAll(vehicleStatus.get("online"));
			}
			LocalCache.onlineVehicles.put("online", onlineList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("=============获取在线，用时：" + (end-start)/1000 + "." + (end-start)%1000 + "秒==============");
	}
}
